import {
	nextSnowflakeId
} from '@/utils/snowflake.js'
import {
	toast,
} from '@/utils/common'
import {
	miniPay
} from '@/api/index.js'
export default function weChatPay(data) {
	console.log('---testPay---')
	let miniPayParam = {
		reqId: nextSnowflakeId(),
		stationId: data.stationId,
		openid: uni.getStorageSync('userOpenId'),
		tradeType: data.tradeType, //RECHARGE_ORDER
		sourceOrderId: data.sourceOrderId,
		attach: '', //
		payAmt: data.payAmt,
		connectorId:data.connectorId,
		body: '测试微信小程序支付',
		paidTypeId: data.paidTypeId,
		paidTypeName: data.paidTypeName
	}
	console.log("star mini pay...")
	return new Promise(function(resolve, reject) {
		miniPay(miniPayParam).then(res => {
			console.log(res)
			const miniPayRes = res
			wx.requestPayment({
				timeStamp: res.timeStamp, //后端返回的时间戳
				nonceStr: res.nonceStr, //后端返回的随机字符串
				package: res.package_str, //后端返回的prepay_id
				signType: res.signType, //后端签名算法,根据后端来,后端MD5这里即为MD5
				paySign: res.paySign, //后端返回的签名
				success(res) {
					console.log('用户支付扣款成功', res)
					resolve({
						code: 200,
						innerTradeNo: miniPayRes.innerTradeNo,
						msg: res
					})
				},
				fail(res) {
					console.log('支付失败', res)
					toast('支付失败')
					resolve({
						code: 100,
						msg: res
					})
				}
			})
		}).catch(err => {
			uni.showModal({
				title: '支付失败',
				content: String(err),
				showCancel: true
			})
			reject({
				title: '支付失败',
				content: String(err),
				showCancel: true
			})
		})
	})
}